class EventCenter{
    //1.事件数组
    handlers = {}

    //2.添加事件
    addEventListener(type, handler) {
        if(!this.handlers[type]) {
            this.handlers[type] = []
        }
        this.handlers[type].push(handler)
    }

    //3.触发事件
    dispatchEvent(type) {
        if(!this.handlers[type]) {
            return new Error("该事件未注册")
        }
        this.handlers[type].forEach(handler => handler())
    }

    //4.移除事件
    removeEvent(type, handler) {
        if(!this.handlers[type]) {
            return new Error("该事件未注册")
        } else {
            const idx = this.handlers[type].findIndex(el => el === handler)
            if(idx === -1) {
                return new Error("未绑定该事件")
            }
            this.handlers[type].splice(idx, 1)
            if(this.handlers[type].length === 0) {
                delete this.handlers[type]
            }
        }
    }
}

//问题： 匿名函数

const f1 = () => console.log(1)
const f2 = () => console.log(2)

let ec = new EventCenter()
ec.addEventListener('click', f1)
ec.addEventListener('click', f2)
ec.dispatchEvent('click')
console.log("删除")
ec.removeEvent('click', f2)
console.log("删除")
ec.dispatchEvent('click')